package com.xianbeiying.app.ckgl.util;

import android.text.format.Time;
import android.view.View;
import android.widget.LinearLayout;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

/**
 * 日期工具类
 */
public class DataUtil {
    //获取系统日期
    private static Time t = new Time();
    //设置默认选中框的数量
    public static ArrayList<Boolean> getList(int size) {
        ArrayList<Boolean> arrayList = new ArrayList<>();
        for (int i = 0; i < size; i++){
            arrayList.add(i, false);
        }
        return arrayList;
    }
    //判断某年某月某天是星期几
    public static int weekDay(String week){
        GregorianCalendar gc=(GregorianCalendar) Calendar.getInstance();
        SimpleDateFormat formt=new SimpleDateFormat(
                "yyyy-MM-dd");
        Date date = null;
        try{
            date = formt.parse(week);
        }catch (ParseException e){
            e.printStackTrace();
        }
        gc.setTime(date);
        //返回值为0的话 为周日 //返回值为6的话 为周六
        return gc.get(Calendar.DAY_OF_WEEK) - 1;
    }
    public static String weekStr(String week){

        return  getWeekText(weekDay(week));
    }
    //获取当月的天数
    public static int getCurrentMonthDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }
    //获取当月的第一天日期
    public static String getOneDay() {
        return getYear() + "-" + getMonth() + "-" + 1 + "";
    }

    //获取系统的年数
    public static int getYear() {
        t.setToNow();
        return t.year;
    }

    //获取系统的月数
    public static int getMonth() {
        t.setToNow();
        return t.month + 1;
    }

    //获取系统的天数
    public static int getDay() {
        t.setToNow();
        return t.monthDay;
    }

    //获取当前系统日期的星期数
    public static int getWeek() {
        t.setToNow();
        return t.weekDay;
    }
    // 解析时间   type  yyyy-MM-dd HH:mm:ss
    public static String paserTime( String time,String type) {
        SimpleDateFormat format = new SimpleDateFormat(type);
        if (time.length()==13) {
            return format.format(new Date(toLong(time)));
        } else {
            return format.format(new Date(toLong(time) * 1000L));
        }
    }

    /**
     * String转long
     *
     * @param obj
     * @return 转换异常返回 0
     */
    public static long toLong(String obj) {
        try {
            return Long.parseLong(obj);
        } catch (Exception e) {
        }
        return 0;
    }
    //添加日期数据
    public static ArrayList<String> getArraayList() {
        ArrayList<String> arrayList = new ArrayList<>();
        int dayMaxNum = getCurrentMonthDay();
        arrayList.clear();
        //当月1号为星期天是添加的数据
        if (DataUtil.weekDay(getOneDay()) == 0) {
            for (int i = 0; i < dayMaxNum; i++) {
                arrayList.add(i + 1 + "");
            }
        } else {
            for (int i = 0; i < DataUtil.weekDay(getOneDay()); i++) {
                arrayList.add(1 + i + "");
            }
            for (int i = 0; i < dayMaxNum; i++) {
                arrayList.add(i + 1 + "");
            }
        }
        return arrayList;
    }

    /**
     * 根据年 月 获取对应的月份 天数
     */
    public static int getDaysByYearMonth(int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, getYear());
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    //计算一年时间里的所有日期并存入数据对象里面
    public static List<String> getDataList() {
        List<String> arrayList = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < getDaysByYearMonth(1 + i); j++) {
                if (i < 9) {
                    if (j < 9) {
                        arrayList.add(getYear() + ".0" + (1 + i) + ".0" + (1 + j));
                    } else {
                        arrayList.add(getYear() + ".0" + (1 + i) + "." + (1 + j));
                    }
                } else {
                    if (j < 9) {
                        arrayList.add(getYear() + "." + (1 + i) + ".0" + (1 + j));
                    } else {
                        arrayList.add(getYear() + "." + (1 + i) + "." + (1 + j));
                    }

                }
            }
        }
        return arrayList;
    }

    public static List<String> getTimeHH(int ids) {
        List<String> arrayList = new ArrayList<>();
        arrayList.clear();
        int length = getTimeList().size();
        for (int i = 0; i < length; i++) {
            String str = getTimeList().get(i);
            String[] sttrr = str.split(":");
            arrayList.add(sttrr[ids]);
        }
        return arrayList;
    }

    //存储一天24小时的时间段
    public static List<String> getTimeList() {
        List<String> arrayList = new ArrayList<>();
        arrayList.clear();
        //小时
        for (int i = 0; i < 24; i++) {
            //分钟
            for (int j = 0; j < 60; j++) {
                if (i < 10) {
                    if (j < 10) {
                        arrayList.add("0" + i + ":0" + j);
                    } else {
                        arrayList.add("0" + i + ":" + j);
                    }
                } else {
                    if (j < 10) {
                        arrayList.add(i + ":0" + j);
                    } else {
                        arrayList.add(i + ":" + j);
                    }
                }
            }
        }
        return arrayList;
    }
    static int ids;
    public static int timeHHid;
    public static List<String> getTimeHHs() {
        List<String> arrayList = new ArrayList<>();
        arrayList.clear();
        String[] strss = tiemStr().split(":");
        //小时
        for (int i = 0; i < 24; i++) {

            if (i < 10) {
                if (("0" + i).equals(strss[0])) {
                    arrayList.add("0" + i + "");
                    timeHHid = i;
                } else {
                    arrayList.add("0" + i);
                }
            } else {
                if ((i + "").equals(strss[0])) {
                    arrayList.add(i + "");
                    timeHHid = i;
                } else {
                    arrayList.add(i + "");
                }
            }
        }
        return arrayList;

    }

    public static int timeMMid;

    public static List<String> getTimeMMs() {
        List<String> arrayList = new ArrayList<>();
        arrayList.clear();
        String[] strss = tiemStr().split(":");
        //小时
        for (int i = 0; i < 60; i++) {

            if (i < 10) {
                if (("0" + i).equals(strss[1])) {
                    arrayList.add("0" + i + "");
                    timeMMid = i;

                } else {
                    arrayList.add("0" + i);
                }


            } else {
                if (("" + i).equals(strss[1])) {
                    arrayList.add(i + "");
                    timeMMid = i;
                } else {
                    arrayList.add(i + "");
                }

            }
        }
        return arrayList;

    }

    //获取当前的时间段对应数组的下标id
    public static int dateId(List<String> list, String content) {
        int length = list.size();

        for (int i = 0; i < length; i++) {
            if (list.get(i).equals(content)) {
                ids = i;
            }
        }
        return ids;
    }

    //获取系统时间
    public static String tiemStr() {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        System.out.println();// new Date()为获取当前系统时间
        return df.format(new Date());
    }

    //获取当前日期
    public static String date;

    public static String dateStr() {
        if (getMonth() < 9) {
            if (getDay() < 9) {
                date = getYear() + ".0" + getMonth() + ".0" + getDay();
            } else {
                date = getYear() + ".0" + getMonth() + "." + getDay();
            }
        } else {
            if (getDay() < 9) {
                date = getYear() + "." + getMonth() + ".0" + getDay();
            } else {
                date = getYear() + "." + getMonth() + "." + getDay();
            }
        }
        return date;
    }

    //添加出生日期中间的横杠
    public static String getDateDesc(String datedesc) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            Date date = formatter.parse(datedesc);
            String sdate = (new SimpleDateFormat("yyyy-MM-dd")).format(date);
            return sdate;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String getRQ(String datedesc) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            Date date = formatter.parse(datedesc);
            String sdate = (new SimpleDateFormat("MM-dd")).format(date);
            return sdate;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getRQs(String datedesc,int ids) {
       if(datedesc.contains("日")){
           String [] rq=datedesc.split("日");
               return getLastTime(ids).replace("-",".")+"\n"+rq[1];
       }
        return datedesc;
    }
    public static String getTemp(String temp,String type,String types){
        String ts=temp.replace(type,"").trim();
        return  ts.replace(types,"").trim();
    };
    //去掉出生日期中间的横杠
    public static String getStrDateDesc(String datedesc) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = formatter.parse(datedesc);
            String sdate = (new SimpleDateFormat("yyyyMMddhhmmss")).format(date);
            return sdate;
        } catch (ParseException e){
            e.printStackTrace();
        }
        return null;
    }

    //去掉出生日期中间的横杠
    public static String getDateDescss(String datedesc) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date date = formatter.parse(datedesc);
            return formatter.format(date);
        } catch (ParseException e){
            e.printStackTrace();
        }
        return null;
    }

    //月
    public static String getMondey(String datedesc) {
        try{
            SimpleDateFormat formatter = new SimpleDateFormat("MM");
            Date date = formatter.parse(datedesc);
            return formatter.format(date);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return null;
    }
    //天
    public static String geDay(String datedesc) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("dd");
            Date date = formatter.parse(datedesc);
            return formatter.format(date);
        } catch (ParseException e){
            e.printStackTrace();
        }
        return null;
    }

    //
    public static Integer getNumberData(String datedesc) {
        return Integer.valueOf(datedesc.replace(".", ""));
    }

    /**
     * 选择行程日期的数据
     */
    public static List<String> getDays() {
        List<String> arrayList = new ArrayList<>();
        for (int i = 0; i < getCurrentMonthDay(); i++) {
            arrayList.add(i + 1 + "");
        }
        return arrayList;
    }

    //获取系统时间yyyy-MM-dd
    public static String dateTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        System.out.println();// new Date()为获取当前系统时间
        return df.format(new Date());
    }
    //获取系统时间yyyy-MM-dd
    public static String dateHTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");//设置日期格式
        System.out.println();// new Date()为获取当前系统时间
        return df.format(new Date());
    }
    //将时间字符串转为时间戳 yyyy-MM-dd HH:mm:ss
    public static int getTime(String trl,String type){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(type);
        Date date = null;
        try {
            date = simpleDateFormat.parse(trl);
            return (int) (date.getTime() / 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取当前日期的后一天
     */
    public static String getLastTime(int day) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.add(Calendar.DAY_OF_YEAR, day);
        Date date = calendar.getTime();
        return sdf.format(date);
    }

    //把字符串转为日期
    public static Date ConverToDate(String strDate)
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            return df.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 获得指定日期的后一天
     *
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay, int postion) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + postion);

        String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayAfter;
    }

    public static String getWeekText(int id) {
        String week = null;
        switch (id) {
            case 0:
                week = "星期天";
                break;
            case 1:
                week = "星期一";
                break;
            case 2:
                week = "星期二";
                break;
            case 3:
                week = "星期三";
                break;
            case 4:
                week = "星期四";
                break;
            case 5:
                week = "星期五";
                break;
            case 6:
                week = "星期六";
                break;
        }
        return week;
    }

    /**
     * 获取日期
     *
     * @param day 负数为  前几天  正数为后几天的时间  0 为当前日期
     * @return
     */
    public static String getDataFouter(int day) {
        Calendar rightNow = Calendar.getInstance();
        SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd");
        //得到当前时间，+3天
        rightNow.add(Calendar.DAY_OF_MONTH, day);
        //进行时间转换
        String date = sim.format(rightNow.getTime());
        System.out.println(date);
        return date;
    }
    //获取时间差
    public static String getSLong(String starttime,String endtime){
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//       final long minute1;
        try {
            Date begin = dfs.parse(starttime);
            java.util.Date end = dfs.parse(endtime);
            long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
//            long day1=between/(24*3600);
//            long hour1=between%(24*3600)/3600;
//            long minute1=between%3600/60;
//            long second1=between%60/60;
//            System.out.println(""+day1+"天"+hour1+"小时"+minute1+"分"+second1+"秒");
            return  String.valueOf(between/60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    //获取时间差
    public static String getHHLong(String starttime,String endtime){
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//       final long minute1;
        try {
            Date begin = dfs.parse(starttime);
            java.util.Date end = dfs.parse(endtime);
            long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
//            long day1=between/(24*3600);
//            long hour1=between%(24*3600)/3600;
//            long minute1=between%3600/60;
//            long second1=between%60/60;
//            System.out.println(""+day1+"天"+hour1+"小时"+minute1+"分"+second1+"秒");
            return  String.valueOf(between/(3600*24));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    //获取时间差
    public static String getHHLongss(String starttime,String endtime){
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
//       final long minute1;
        try {
            Date begin = dfs.parse(starttime);
            java.util.Date end = dfs.parse(endtime);
            long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
//            long day1=between/(24*3600);
//            long hour1=between%(24*3600)/3600;
//            long minute1=between%3600/60;
//            long second1=between%60/60;
//            System.out.println(""+day1+"天"+hour1+"小时"+minute1+"分"+second1+"秒");
            return  String.valueOf(between/(3600*24));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static  void setHeight(View view, int width){
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) view.getLayoutParams(); //取控件textView当前的布局参数
//                int width = AppManager.getInstance().getWindowSizeEntity().getWidth();
        linearParams.width =width;
        view.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
    }
   //设置1小时 1个小时后台退出登录
    public static String setOneTime(int time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        System.out.println("当前时间：" + sdf.format(now));
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, time);
        System.out.println(sdf.format(nowTime.getTime()));
        return  sdf.format(nowTime.getTime());
    }
    //获取当前时间点
    public static String getLoactionTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date now = new Date();
        System.out.println("当前时间：" + sdf.format(now));
        return  sdf.format(now);
    }

    /**
     * 获取指定网站的日期时间
     *
     * @param webUrl
     * @return
     * @author SHANHY
     * @date   2015年11月27日
     */
    public static String getWebsiteDatetime(String webUrl){
        try {
            URL url = new URL(webUrl);// 取得资源对象
            URLConnection uc = url.openConnection();// 生成连接对象
            uc.connect();// 发出连接
            long ld = uc.getDate();// 读取网站日期时间
            Date date = new Date(ld);// 转换为标准时间对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);// 输出北京时间
            return sdf.format(date);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
